package com.security.dataprocess.utils;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.rmi.ServerException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Describe
 *
 * @author firr
 * @date 2022年07月12日15:10
 */
public class DateUtil {
    /**
     * 默认日期格式
     */
    private static final String DATE_DEFAULT = "yyyy-MM-dd";
    /**
     * 完整日期格式
     */
    private static final String DATE_DEFAULT_FULL = "yyyy-MM-dd HH:mm:ss";
    /**
     * 日期格式:年-月
     */
    private static final String MONTH_DATE_DEFAULT = "yyyy-MM";
    /**
     * 日期格式:时间毫秒
     */
    private static final String DATE_TIME_MILLIS = "yyyyMMddhhmmss";

    public static LocalDate dateToLocalDate(Date date) {
        if (EmptyUtil.isEmpty(date)) {
            date = new Date();
        }

        Instant instant = date.toInstant();
        return instant.atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static String dayStrBetweenLocalDate(LocalDate startDate, LocalDate endDate) {

        if (EmptyUtil.isEmpty(startDate) || EmptyUtil.isEmpty(endDate)) {
            return null;
        }

        return (endDate.toEpochDay() - startDate.toEpochDay()) + "天";
    }

    /**
     * String转LocalDateTime,日期格式目前仅以长度判断,不符合将抛日期不支持异常
     * @author firr
     * @date 2022/7/18 10:55
     * @param str 日期字符串
     * @return java.time.LocalDateTime
     */
    public static LocalDateTime strToLocalDateTime(String str) throws ServerException{
        DateTimeFormatter df;
        if (str.length() == 10) {
            df = DateTimeFormatter.ofPattern(DATE_DEFAULT);
            return LocalDateTime.parse(str, df);
        } else if (str.length() == 19) {
            df = DateTimeFormatter.ofPattern(DATE_DEFAULT_FULL);
            return LocalDateTime.parse(str, df);
        }
        throw new RuntimeException();
    }

    public static String localDateTimeToDefaultStr(LocalDateTime localDateTime) {
        if (EmptyUtil.isEmpty(localDateTime)) {
            return null;
        }


        return localDateTime.format(DateTimeFormatter.ofPattern(DATE_DEFAULT));
    }

    public static String localDateTimeToTimeMilliStr(LocalDateTime localDateTime) {
        if (EmptyUtil.isEmpty(localDateTime)) {
            return null;
        }

        return localDateTime.format(DateTimeFormatter.ofPattern(DATE_TIME_MILLIS));
    }

    public static String localDateTimeToMonthStr(LocalDateTime localDateTime) {
        if (EmptyUtil.isEmpty(localDateTime)) {
            return null;
        }

        return localDateTime.format(DateTimeFormatter.ofPattern(MONTH_DATE_DEFAULT));
    }

    public static String localDateTimeToDefaultFullStr(LocalDateTime localDateTime) {
        if (EmptyUtil.isEmpty(localDateTime)) {
            return null;
        }

        return localDateTime.format(DateTimeFormatter.ofPattern(DATE_DEFAULT_FULL));
    }

    public static long getDaysBetweenDate(LocalDate endDate, LocalDate startDate) {
        return endDate.toEpochDay() - startDate.toEpochDay();
    }

    /**
     * @description
     * date类型时间戳转换成标准日期格式字符串
     * @author junjie
     * @since 2022/8/1 10:28
     */
    public static String dateToDefaultFull(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        return format;
    }

    /**
     * @description
     * 带时区string类型时间转换成标准日期格式字符串
     * @author junjie
     * @return String
     * @since 2022/8/1 10:28
     */
    public static String strToDefaultFull(String date) {
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+08:00");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(ObjectUtils.isNotEmpty(date)) {
            Date formatDate = null;
            try {
                formatDate = dateFormat1.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return dateFormat2.format(formatDate);
        }
        return null;
    }

    /**
     * @description
     * 带时区string类型时间转换成Date
     * @author junjie
     * @return String
     * @since 2022/8/1 10:28
     */
    public static Date strToDate(String date) {
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+08:00");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(ObjectUtils.isNotEmpty(date)){
            Date formatDate = null;
            try {
                formatDate = dateFormat1.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return formatDate;
        }
        return null;
    }

    /**
     * @description
     * long类型时间戳转date
     * @author junjie
     * @since 2022/8/10 15:13
     */
    public static Date longToDate(long millis){
        Date date = new Date();
        date.setTime(millis);
        return date;
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }


    public static void main(String[] args) {
        System.out.println(DATE_DEFAULT.length());
        System.out.println(DATE_DEFAULT_FULL.length());
    }
}
